package com.ruoyi.web.controller.admin.project;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.dto.ProjectDTO;
import com.ruoyi.system.dto.ProjectQueryDTO;
import com.ruoyi.system.dto.ProjectAttachmentDownloadDTO;
import com.ruoyi.system.dto.ProjectFileQueryDTO;
import com.ruoyi.system.service.ProjectService;
import com.ruoyi.system.service.IProjectAttachmentService;
import com.ruoyi.system.service.IProjectFileService;
import com.ruoyi.system.vo.ProjectAttachmentGroupVO;
import com.ruoyi.system.vo.ProjectAttachmentVO;
import com.ruoyi.system.vo.ProjectFileListVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 项目控制器
 */
@RestController
@RequestMapping("/admin/project")
@Api(tags = "项目管理")
public class ProjectController extends BaseController {

    @Resource
    private ProjectService projectService;

    @Resource
    private IProjectAttachmentService projectAttachmentService;

    @Resource
    private IProjectFileService projectFileService;

    @PostMapping("/add")
    @ApiOperation("创建项目")
    public AjaxResult createProject(@Validated @RequestBody ProjectDTO projectDTO) {
        return AjaxResult.success(projectService.createProject(projectDTO));
    }

    @PutMapping("/{id}")
    @ApiOperation("更新项目")
    public AjaxResult updateProject(@PathVariable Long id,
                                            @Validated @RequestBody ProjectDTO projectDTO) {
        return AjaxResult.success(projectService.updateProject(id, projectDTO));
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除项目")
    public AjaxResult deleteProject(@PathVariable Long id) {
        return AjaxResult.success(projectService.deleteProject(id));
    }

    @GetMapping("/{id}")
    @ApiOperation("根据ID查询项目")
    public AjaxResult getProjectById(@PathVariable Long id) {
        return AjaxResult.success(projectService.getProjectById(id));
    }

    @PostMapping("/page")
    @ApiOperation("分页查询项目列表")
    public AjaxResult queryProjectPage(@RequestBody ProjectQueryDTO queryDTO) {
        return AjaxResult.success(projectService.queryProjectPage(queryDTO));
    }

    @GetMapping("/{projectId}/attachments")
    @ApiOperation("获取项目所有附件（按分类分组）")
    public AjaxResult getProjectAttachmentsByGroup(@PathVariable Long projectId) {
        List<ProjectAttachmentGroupVO> attachments = projectAttachmentService.getProjectAttachmentsByGroup(projectId);
        return AjaxResult.success(attachments);
    }

    @GetMapping("/{projectId}/attachments/list")
    @ApiOperation("获取项目所有附件（平铺列表）")
    public AjaxResult getProjectAttachments(@PathVariable Long projectId) {
        List<ProjectAttachmentVO> attachments = projectAttachmentService.getProjectAttachments(projectId);
        return AjaxResult.success(attachments);
    }

    @GetMapping("/{projectId}/attachments/type/{attachmentType}")
    @ApiOperation("根据附件类型获取项目附件")
    public AjaxResult getProjectAttachmentsByType(@PathVariable Long projectId, @PathVariable Integer attachmentType) {
        List<ProjectAttachmentVO> attachments = projectAttachmentService.getProjectAttachmentsByType(projectId, attachmentType);
        return AjaxResult.success(attachments);
    }

    @PostMapping("/attachments/download")
    @ApiOperation("批量下载项目附件")
    public void downloadProjectAttachments(@Validated @RequestBody ProjectAttachmentDownloadDTO downloadDTO, HttpServletResponse response) {
        projectAttachmentService.downloadProjectAttachments(downloadDTO, response);
    }

    @PostMapping("/file/list")
    @ApiOperation("分页查询项目文件管理列表")
    public AjaxResult getProjectFileList(@RequestBody ProjectFileQueryDTO queryDTO) {
        return AjaxResult.success(projectFileService.getProjectFileList(queryDTO));
    }
}